<template>
  <div id="SpaceManagePage">
    <a-flex justify="space-between">
      <h2 style="padding-bottom: 16px; font-weight: 550">空间管理</h2>
      <a-space>
        <a-button type="primary" href="/add_space" target="_blank">+ 创建空间</a-button>
      </a-space>
    </a-flex>
    <a-form :model="searchParams" layout="inline" @finish="doSearch" style="padding-bottom: 20px">
      <a-form-item label="空间名称">
        <a-input v-model:value="searchParams.spaceName" placeholder="请输入空间名称" />
      </a-form-item>
      <a-form-item label="空间名">
        <a-select
          v-model:value="searchParams.spaceLevel"
          :options="SPACE_LEVEL_OPTIONS"
          placeholder="请选择空间级别"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-button type="primary" htmlType="submit">搜索</a-button>
    </a-form>

    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
      :isloading="loading"
    >
      <template #headerCell="{ column }">
        <template v-if="column.key === 'name'">
          <span>
            <smile-outlined />
            Name
          </span>
        </template>
      </template>

      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'spaceLevel'">
          <div class="space-level">
            <div v-if="record.spaceLevel === 0">
              <a-tag color="green">普通版</a-tag>
            </div>
            <div v-else-if="record.spaceLevel === 1">
              <a-tag color="blue">专业版</a-tag>
            </div>
            <div v-else>
              <a-tag color="red">旗舰版</a-tag>
            </div>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'spaceUseInfo'">
          <div>
            大小: {{ convertByteToMb(record.totalSize) }} / {{ convertByteToMb(record.maxSize) }} MB
            <br />
            数量: {{ record.totalCount }} / {{ record.maxCount }}
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          <span>{{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'editTime'">
          <span>{{ dayjs(record.editTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
        <template v-else-if="column.key === 'action'">
          <a-button
            type="link"
            :href="`/add_space?id=${record.id}`"
            target="_blank"
            style="margin-right: 8px"
            >编辑</a-button
          >
          <a-popconfirm
            title="确定要删除空间吗？"
            @confirm="() => doDelete(record.id)"
            okText="确定"
            cancelText="取消"
          >
            <a-button danger type="link">删除</a-button>
          </a-popconfirm>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import { listSpaceVoPageUsingPost } from '@/api/spaceController'
import { SPACE_LEVEL_OPTIONS } from '@/constants/space'
import dayjs from 'dayjs'
import { computed, onMounted, reactive, ref } from 'vue'
//列名称
const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '空间名称',
    dataIndex: 'spaceName',
  },
  {
    title: '空间级别',
    dataIndex: 'spaceLevel',
  },
  {
    title: '使用情况',
    dataIndex: 'spaceUseInfo',
  },
  {
    title: '空间 id',
    dataIndex: 'userId',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

const dataList = ref([])
const total = ref()

//搜索条件
const searchParams = reactive<API.SpaceQueryRequest>({
  current: 1,
  pageSize: 10,
})

//分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

//获取空间列表
const getUserList = async () => {
  const res = await listSpaceVoPageUsingPost(searchParams)
  console.log('获取空间列表', res)
  if (res.data.data && res.data.code === 0) {
    dataList.value = res.data.data.records
    total.value = res.data.data.total
  }
}

//切换表格页数
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  getUserList()
}
//删除空间
const doDelete = async (id: number) => {
  const res = await deleteUserUsingPost({ id })
  console.log('删除空间', res)
  if (res.data.code === 0 && res.data.data) {
    getUserList()
  }
}
//搜索
const doSearch = () => {
  searchParams.current = 1
  console.log('搜索', searchParams)
  getUserList()
}

//格式化字节为MB
const convertByteToMb = (byteValue: number | string | null | undefined) => {
  if (byteValue === null || byteValue === undefined) {
    return '0'
  }
  const byte = Number(byteValue)
  if (isNaN(byte)) {
    return '0'
  }
  const mb = byte / (1024 * 1024)
  return mb
    .toFixed(2)
    .replace(/\.00$/, '')
    .replace(/(\.\d)0$/, '$1')
}
onMounted(() => {
  getUserList()
})
</script>

<style lang="scss" scoped></style>
